package entities;

import view.ChessBoard;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * Author: Mr.Zheng, Time: 2018/5/7/9:24
 * Description: 炮
 */

public class Pao extends BasePiece {
    public Pao(int x, int y, Color color) {
        super(x, y, color);
        this.pieceName = "炮";
    }

    @Override
    public void updateAvailablePoints(Map<Point, BasePiece> pointPieceMap) {
        availablePoints.clear();
        int x = getPosition().x;
        int y = getPosition().y;
        //先定义上下左右四个变量
        int up = 0;
        int down = 0;
        int left = 0;
        int right = 0;
        //先向上找，如果为空标记改点
        for (int yi = y - 1; yi >= 0; yi--) {
            BasePiece piece = pointPieceMap.get(new Point(x, yi));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(x, yi));
            }
            //否则记录下这个点y坐标，结束循环
            else {
                up = yi;
                break;
            }
        }
        //从记录下那个点开始向上找，如果非空且是敌方棋子的话，标记改点
        for (int yii = up - 1; yii >= 0; yii--) {
            BasePiece piece1 = pointPieceMap.get(new Point(x, yii));
            if (piece1 != null && piece1.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x, yii));
                break;
            }
        }

        //先向下找，如果为空标记改点
        for (int yi = y + 1; yi <= 9; yi++) {
            BasePiece piece = pointPieceMap.get(new Point(x, yi));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(x, yi));
            }
            //否则记录下这个点y坐标，结束循环
            else {
                down = yi;
                break;
            }
        }
        //从记录下那个点开始向下找，如果非空且是敌方棋子的话，标记改点
        for (int yii = down + 1; yii <= 9; yii++) {
            BasePiece piece1 = pointPieceMap.get(new Point(x, yii));
            if (piece1 != null && piece1.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x, yii));
                break;
            }
        }

        //先向左找，如果为空标记改点
        for (int xi = x - 1; xi >= 0; xi--) {

            BasePiece piece = pointPieceMap.get(new Point(xi, y));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(xi, y));
            }
            //否则记录下这个点x坐标，结束循环
            else {
                left = xi;
                break;
            }
        }
        //从记录下那个点开始向左找，如果非空且是敌方棋子的话，标记改点
        for (int xii = left - 1; xii >= 0; xii--) {
            BasePiece piece1 = pointPieceMap.get(new Point(xii, y));
            if (piece1 != null && piece1.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(xii, y));
                break;
            }
        }

        //先向右找，如果为空标记改点
        for (int xi = x + 1; xi <= 8; xi++) {
            BasePiece piece = pointPieceMap.get(new Point(xi, y));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(xi, y));
            }
            //否则记录下这个点x坐标，结束循环
            else {
                right = xi;
                break;
            }
        }
        //从记录下那个点开始向右找，如果非空且是敌方棋子的话，标记改点
        for (int xii = right + 1; xii <= 8; xii++) {
            BasePiece piece1 = pointPieceMap.get(new Point(xii, y));
            if (piece1 != null && piece1.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(xii, y));
                break;
            }
        }
    }
}
